require 'open-uri'
require 'rss/1.0'
require 'rss/2.0'
require 'rss/maker'

def rss(context, config)
  return true  unless config.key?(:link)

# item_names = %w(title link description about content author enclosure pubDate dc_date dc_creator)
  item_names = %w(title link description date content enclosure)

  entries = []

  config[:link].each {|url|
    text = ''
    open(url) {|io|
      text = io.read
    }

    begin
      rss = RSS::Parser.parse(text)
    rescue RSS::InvalidRSSError
      rss = RSS::Parser.parse(text, false)
    end

    rss.items.each {|i|
      entry = Rire::Entry.new
      item_names.each {|name|
        val = i.send(name) rescue nil
#       name.to_sym] = val unless val.nil?
        entry.send(name, val) unless val.nil?
      }
      entries << entry
    }
  }

  context.entries = entries
end

